<style>
#returnData .layui-table-cell{overflow: inherit;}
.lay-pic{max-height:70px;}
.sign img{max-height:40px;}
.layui-table-cell{padding:1px 2px;line-height:21px;}
.layui-form-pane .layui-form-label{padding:8px 12px;width:auto;}
.layui-form-item .layui-input-inline{width:auto;}
.laytable-cell-1-0-12{white-space:normal;}
</style>
<div class="fixed-bar">
    <form class="layui-form layui-form-pane fixed-bar-form" action="{:url('',['status' => $status])}" method="get" id="hisi-table-search">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">订单号/采购批号</label>
                <div class="layui-input-inline">
                    <input type="text" name="client_ordernum" lay-verify="" placeholder="输入订单号" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
               <label class="layui-form-label ">退料日期</label>
               <div class="layui-input-inline">
                   <input type="text" name="create_date_range" readonly="" id="create_date_range" lay-verify="" placeholder="退料创建日期" class="layui-input">
               </div>
           </div>
            <div class="layui-inline">
               <label class="layui-form-label ">回料日期</label>
               <div class="layui-input-inline">
                   <input type="text" name="giveback_time" readonly="" id="giveback_time" lay-verify="" placeholder="选择回料日期" class="layui-input">
               </div>
           </div>
            <div class="layui-inline removeStat">
                <label class="layui-form-label">工厂</label>
                <div class="layui-input-inline" style="width:130px;">
                    {:Form::select('factory_id', '', $factoryKV, '', '')}
                </div>
            </div>
            <div class="layui-inline">
               <label class="layui-form-label">供应商</label>
                <div class="layui-input-inline">
                    {:Form::select('stall_id','' , $stallsKV, 'stall-ids', 'layui-input','lay-search=""')}
                </div>
            </div>
            <div class="layui-inline">
               <label class="layui-form-label">发起人</label>
                <div class="layui-input-inline">
                    <input type="text" name="creater" lay-verify="" placeholder="发起人关键字" style="width:120px;" class="layui-input">
                </div>
            </div>
           <div class="layui-inline">
               <label class="layui-form-label">退料原因</label>
                <div class="layui-input-inline">
                    <input type="text" name="reason" lay-verify="" placeholder="原因关键字" style="width:120px;" class="layui-input">
                </div>
            </div>
			<div class="layui-inline">
                <button type="submit" id="seacher" class="layui-btn" lay-submit="" lay-filter="demo1">搜索</button>
                <!-- <button type="button" class="layui-btn layui-btn-primary hisi-export">导出</button>-->
            </div>
        </div>
    </form>
</div>
<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
  <ul class="layui-tab-title">
    <li><a onclick="statCli('')" href="#">全部</a></li>
    <li class="layui-this"><a onclick="statCli(0)" href="#">待处理（<span class="red bold stat-num-0">{:arrayGet($statData, 0, 0)}</span>条）</a></li>
    <li><a onclick="statCli(1)" href="#">换料中（<span class="red bold stat-num-1">{:arrayGet($statData, 1, 0)}</span>条）</a></li>
    <li><a onclick="statCli(2)" href="#">待审核（<span class="red bold stat-num-2">{:arrayGet($statData, 2, 0)}</span>条）</a></li>
    <li><a onclick="statCli(3)" href="#">已完成（<span class="stat-num-3">{:arrayGet($statData, 3, 0)}</span>条）</a></li>
    <li><a onclick="statCli(4)" href="#">出库打印</a></li>
    <li><a onclick="statCli(5)" href="#">出库记录</a></li>
  </ul>
    <div class="layui-tab-content">
        <div id="returnData"><table id="dataTable" lay-filter="dataTable"></table></div>
        
        <div id="deliverLog">
            
        </div>
    </div>
</div>      



{include file="system@block/layui" /}

<script type="text/html" title="操作按钮模板" id="buttonTpl">
	<div class="btngroup-{{ d.id }}" align='center'>
	{{# if(d.status == 0 || d.status == 1){ }}
        <a href="{:url('rtClothDetail')}?order_material_id={{ d.order_material_id }}&material_sku={{ d.material_sku }}&a=add&status={{d.status}}" title="入库" class="layui-btn layui-btn-xs layui-btn-normal hisi-iframe" data-id="{{ d.id }}" hisi-data="{width: '800px', height: '600px'}">入库</a>

        <a href="{:url('rtClothDetail')}?order_material_id={{ d.order_material_id }}&status={{d.status}}&val=2" title="退料" class="layui-btn layui-btn-warm layui-btn-xs layui-btn-normal hisi-iframe" data-id="{{ d.id }}" hisi-data="{width: '800px', height: '500px'}">退料</a>
		
		{{# if(d.status == 0){ }}
            <a href="{:url('rtClothDetail')}?order_material_id={{ d.order_material_id }}&status={{d.status}}&val=1" title="换料" class="layui-btn layui-btn-warm layui-btn-xs layui-btn-normal hisi-iframe" data-id="{{ d.id }}" hisi-data="{width: '800px', height: '500px'}">换料</a>
		{{# } }}
	{{# } else if(d.status == 2) { }}
        <a id="shenhe1" href="{:url('rtClothDetail')}?order_material_id={{ d.order_material_id }}&status={{d.status}}&val=3&a=audit" class="layui-btn layui-btn-xs layui-btn-normal hisi-iframe" hisi-data="{width: '800px', height: '500px'}">审核</a>
        
	{{# } else if(d.status == 3) { }}
		<a href="{:url('log')}?id={{ d.id }}" title="操作日志" class="layui-btn layui-btn-xs layui-btn-primary hisi-iframe" hisi-data="{width: '500px', height: '400px'}">操作日志</a>
	{{# } }}
	</div>
</script>
<script type="text/html" id="picTpl">
    <span class="lay-pic-container" src="{{ d.sInfo.pic }}">
		<img src="{{ d.sInfo.ori_pic }}" class="lay-pic">
		{{# if(d.status==99){ }}
			<span class="sign"><img src="/static/system/image/sign_delete.png"></span>
		{{# }else if(d.status==2){ }}
			<span class="sign"><img src="/static/system/image/sign_over.png"></span>
		{{# }else if(d.tail_status==1){ }}
			<span class="sign"><img src="/static/system/image/sign_tail.png"></span>
		{{# }else if(d.chejian_status==1){ }}
			<span class="sign"><img src="/static/system/image/sign_workshop.png"></span>
		{{# }else if(d.caichuang_status==1){ }}
			<span class="sign"><img src="/static/system/image/sign_cutting.png"></span>
		{{# }else if(d.caigou_status==1){ }}
			<span class="sign"><img src="/static/system/image/sign_purchasing_done.png"></span>
		{{# }else { }}
			<span class="sign"><img src="/static/system/image/sign_purchasing_todo.png"></span>
		{{# } }}
	</span>
</script>
<script type="text/html" id="skuTpl">
    {{# if (d.stock_m_type==0){ }}
    <span class="copy-item">{{ d.client_ordernum }}</span> <span class="layui-badge layui-bg-blue">{{ layui.tool.formatOrderOType(d.o_type) }}</span><span class="layui-badge layui-bg-orange">{{ layui.tool.formatOrderType(d.type) }}</span><br>
    <span class="copy-item">{{ d.sku }}</span> <br>
    {{# if(d.status == 99){ }}<span class="layui-badge">{{ d.delete_text }}</span> {{# } }}
    {{ d.is_auto_import_name }}
    {{# }else { }}
    <span class="copy-item">B{{ d.order_material_id }}</span><br>
    备料
    {{# } }}
</script>
<script type="text/html" id="UserTpl">
    {{# if(d.duty_admin_name != ''){ }}审批：{{ d.duty_admin_name }} <br> {{# } }}
    {{# if(d.audit_admin_name != ''){ }}审核：{{ d.audit_admin_name }} <br> {{# } }}
</script>
<script type="text/html" id="statusTpl">
    {{# if(d.status==0){ }}
        待处理
    {{# }else if(d.status==1){ }}
        换料中
    {{# }else if(d.status==2){ }}
        待审核
    {{# }else if(d.status==3){ }}
        已完成
    {{# } }}
</script>
<script type="text/html" id="pTpl">
<a href="{:url('rtClothDetail')}?order_material_id={{ d.order_material_id }}&a=check&status={{d.status}}" title="布料详情" class="hisi-iframe layui-btn layui-btn-xs layui-btn-normal" data-id="{{ d.id }}" hisi-data="{width: '800px', height: '500px',endRefresh:'layer'}">{{d.rt_tiaoshu}}匹</a>
</script>
<script type="text/html" id="datetimeTpl">
{{layui.util.toDateString(d.order_create_time*1000,'yyyy-MM-dd HH:mm')}}<br />
<span>货期:{{layui.util.toDateString(d.shipment_time*1000,'yyyy-MM-dd')}}</span><br />
{{layui.util.toDateString(d.create_time*1000,'yyyy-MM-dd HH:mm')}}<br />
</script>
<script type="text/html" id="givebackTimeTpl">
    <input type="text" name="stall_order_time" style="width: 90px;border: none" readonly="" dataid="{{d.id}}" id="stall_order_time{{d.id}}" value="{{d.giveback_time}}" lay-verify="" class="layui-input stall_order_time" lay-event="stall_order_time" style="border: none">
</script>
<script type="text/html" id="stallIdTpl">
    <div class="stall-select">
        {:Form::select('stall_id','', $stallsKV, 'stall-ids-{literal}{{d.id}}{/literal}', 'layui-input stall-ids','lay-search="" lay-filter="stallIds" lay-verify="required" data-id="{literal}{{d.order_material_item_id}}{/literal}"')}
    </div>
</script>
<script type="text/html" id="materialInfoTpl">
{{layui.tool.formatMaterialType(d.rttype, '', 2)}}
<span class="copy-item">{{ d.name }}</span> <br>
<span class="copy-item">{{ d.model }}</span> <br>
<span class="copy-item">{{d.colornum}} / {{d.color}}</span>
</script>
<script type="text/javascript">
    layui.use(['table', 'opTable', 'laydate', 'form', 'laytpl','cookie','tool','layer'], function() {
        var $ = layui.jquery;
        var table = layui.table;
        var laydate = layui.laydate;
        var form = layui.form;
        var layer = layui.layer;
        laydate.render({
		    elem: '#create_date_range'
		    ,range: '~'
		    ,theme: 'molv'
		    ,mark: {'{:$today}':''}
	    	,change: function(value, date, endDate){
		    	$('#create_date_range').val(value);
            	$('#layui-laydate1').remove();//关闭面板
            }
		});
		laydate.render({
		    elem: '#giveback_time'
		    ,range: '~'
		    ,theme: 'molv'
		    ,mark: {'{:$today}':''}
	    	,change: function(value, date, endDate){
		    	$('#giveback_time').val(value);
            	$('#layui-laydate2').remove();//关闭面板
            }
		});
		
        var formData;
        form.on('submit(demo1)', function (data) {
            //表单数据formData
            formData = data.field;
             
        });
        //自定页
        window.statCli = function(status) {
            if(status==4 ){
                $('#layui-table-page1').attr('id', 'layui-table-page2');
                $("#deliverLog").show();
                $("#returnData,.fixed-bar").hide();
                $("#deliverLog").load("{:url('deliveryPrint')}");
            }else if(status==5){
                $('#layui-table-page2').attr('id', 'layui-table-page3');
                $('#layui-table-page1').attr('id', 'layui-table-page2');
                $("#deliverLog").show();
                $("#returnData,.fixed-bar").hide();
                $("#deliverLog").load("{:url('deliveryLog')}");
            }else{
                $('#returnData,.fixed-bar').show();
                $('#deliverLog').hide();
                var url='{:url()}?status='+status;
                $("#hisi-table-search").attr('action',url)
                $("#seacher").click();
            }
            form.render();
		}

        var opTable = layui.opTable.render({
            elem: '#dataTable'
            ,url: '{:url("", ["status" => $status])}' //数据接口
            //,id:'dataTable'
            ,page: true //开启分页
            ,even: true
            ,limit: 20
            ,limits: [20,50,100,150,200]
            ,text: {
                none : '暂无相关数据'
            }
            ,toolbar: '#toolbar'
			,openVisible: false
            //,cellMinWidth: 80
            ,defaultToolbar: ['filter', 'exports', 'print']
            ,cols: [[ //表头
                {field:'pic',title : '图片', templet:'#picTpl',width:80}
				,{field:'admin_id_name',title : '发起人', width:80}
				,{field: 'create_time', title: '下单/货期/退料日期', templet:'#datetimeTpl',width:130}
                ,{field:'client_ordernum',title : '订单号/SKU/帐号', templet:'#skuTpl',width:160}
                ,{field:'factory_name',title : '工厂', templet:'#factoryTpl',width:96}
				,{field: 'name', title: '材料名称/型号/色号/颜色',templet:'#materialInfoTpl',width:200}
				,{field:'supplier',title : '供应商',templet:'#stallIdTpl',width:130}
				,{field: 'model', title: '退换量',templet:'#pTpl'}
                ,{field:'order_time',title : '退换原因', templet:function(d){return d.reason}}
				,{field: 'model', title: '退布类型', templet: function(d){return d.rt_type}}
				,{field: 'model', title: '退料米数', templet: function(d){return d.return_length}}
				,{field: 'giveback_time', title: '回料时间',style:'border-bottom:2px dotted #5FB878;',templet:'#givebackTimeTpl',width:95}
				,{field: 'remark', title: '处理备注',edit:'text',style:'border-bottom:2px dotted #5FB878;'}
                ,{field:'order_user',title : '审批人', templet:'#UserTpl'}
                ,{field:'status',title : '状态', templet:'#statusTpl'} 
				,{field: '', title: '操作', templet: '#buttonTpl',width:150} 
            ]]
            ,done: function(res, curr, count){
				var data=res.data;
                for (var i in data){
                    $('#stall-ids-'+data[i]['id']).val(data[i]['stalls_id']);
                }
                layui.form.render();
				for (var i in data){
					var nowtime=new Date();
					var dataid=data[i]['id'];
                    //if(data[i]['giveback_time']=='') value=nowtime.getFullYear()+"-"+(nowtime.getMonth()+1)+"-01";
                    laydate.render({
                        elem: "#stall_order_time"+data[i]['id'], //指定元素
                        value: '',
						 done: function(value, date){
							var str=this.elem.selector;
							var dataid=str.replace(/#stall_order_time/, "");
							$.ajax({
								type: "POST",
								url: "{:url('status')}",
								data: "id="+dataid+"&action=setFieldVal&field=giveback_time&fieldval="+value,
								dataType:"json",
								success: function(res){
									if (res.code == 1) {
										layer.msg(res.msg, {icon: 1,time:600});
									} else {
										layer.msg(res.msg, {icon: 2,anim: 6,time:600});
									}
								}
							});
						 }
                    });
                    $('#stall_order_time'+data[i]['id']).val(data[i]['giveback_time']);
                }
                //layui.form.render();
				
                opTable.openAll();
                if (res.statData != []) {
                    var total=0,stat=res.statData,abolishNum=res.abolishNum;
                    for(var i in stat) {
                         total= total+Number(stat[i]);
                    }
                    $('.stat-num-all').text(total);
                    $('.stat-num-0').text((stat[0]?stat[0]:0)-(abolishNum[0]?abolishNum[0]:0));
                    $('.stat-num-1').text((stat[1]?stat[1]:0)-(abolishNum[1]?abolishNum[1]:0));
                    $('.stat-num-2').text((stat[2]?stat[2]:0)-(abolishNum[2]?abolishNum[2]:0));
                    $('.stat-num-3').text((stat[3]?stat[3]:0)-(abolishNum[3]?abolishNum[3]:0));
                }
            }
        });
        table.on('edit(dataTable)', function(obj){  //编辑备注
            var value = obj.value //得到修改后的值
                ,d = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            var id=d.id;
			$.ajax({
				type: "POST",
				url: "{:url('status')}",
				data: "id="+d.id+"&action=setFieldVal&field="+field+"&fieldval="+value,
				dataType:"json",
				success: function(res){
					if (res.code == 1) {
						layer.msg(res.msg, {icon: 1,time:600});
					} else {
						layer.msg(res.msg, {icon: 2,anim: 6,time:600});
					}
				}
			});
        });
		form.on('select(stallIds)', function(data){   
                var val=data.value,id=data.elem.id;
                var elem=($('#'+id));
                $.ajax({
                   type: "POST",
                   url: "{:url('status')}",
                   data:"id="+elem.attr("data-id")+"&action=setFieldVal&field=stalls_id&fieldval="+elem.val(),
                   dataType:"json",
                   success: function(res){
                        if (res.code == 1) {
                            layer.msg(res.msg, {icon: 1});
                        } else {
                             layer.msg(res.msg, {icon: 2,anim: 6});
                        }
                   }
                });
            });
		window.setVal=function(tag){
			alert($(tag).attr("dataid"));
		}

        $('#deliverLog').hide();
    });
</script>